package com.imooc.infra.mapping;

import com.imooc.business.constants.SymbolConstants;
import com.imooc.business.domain.Calculate;
import com.imooc.business.domain.Storage;
import com.imooc.infra.repository.entity.TDataCenterCalculate;
import com.imooc.infra.repository.entity.TDataCenterStorage;
import org.apache.commons.collections4.CollectionUtils;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Named;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @Author Tim
 * @Description:
 * @Version 1.0
 *
 *
 * componentModel 可以让mapper spring管理
 *
 */
@Mapper(componentModel = "spring")
public interface CalculateMapping {
    @Mapping(source = "parameters", target = "parameters", qualifiedByName = "toParmString")
    TDataCenterCalculate toEntity(Calculate calculate);

    @Mapping(source = "parameters", target = "parameters", qualifiedByName = "toParmList")
    Calculate toVo(TDataCenterCalculate calculate);

    @Named("toParmString")
    default String toParmString(List<String> params){
        if (CollectionUtils.isEmpty(params)) {
            return "";
        }
        return String.join(SymbolConstants.COMMA, params);
    }

    @Named("toParmList")
    default List<String> toParmList(String params){
        return Arrays.asList(params.split(SymbolConstants.COMMA));
    }
}
